package com.yvan.eventsourcing;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * Created by Administrator on 2018/12/7.
 */
public class EventTemplate extends RabbitTemplate {

    private String version;

    public EventTemplate(ConnectionFactory connectionFactory) {
        super(connectionFactory);
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public void convertAndSend(Object object) throws AmqpException {
        MessageProperties properties = new MessageProperties();
        properties.setHeader("version", version);
        Message message = getMessageConverter().toMessage(object, properties);
        super.convertAndSend(message);
    }
}
